package com.youyi.flymewallpager.drawable;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;

/**
 * Created by xzp on 16/7/6.
 */
public class StarDrawable extends Drawable{
    private int mPointX;
    private int mPointY;
    private int mSize;
    private int mAlpha;
    private Paint mPaint;
    private boolean mLighting;
    private int mSpeed;
    private Path mPath;
    public StarDrawable() {
        mPaint=new Paint();
        mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.FILL);
        mAlpha=0;
        mLighting=true;
        mSpeed= (int) (5+20*Math.random());
        mPath=new Path();
    }

    @Override
    public void draw(Canvas canvas) {
        if(mLighting)
            mAlpha+=mSpeed;
        else
            mAlpha-=mSpeed;
        if(mAlpha<0){
            mAlpha=0;
            mLighting=true;
            mPointX= (int) (canvas.getWidth()*Math.random());
            mPointY= (int) (canvas.getHeight()*Math.random());
            int minSize= (int) (canvas.getWidth()*0.05f);
            mSize= (int) (minSize+minSize*2*Math.random());
            mSpeed= (int) (5+20*Math.random());
        }else if(mAlpha>255){
            mAlpha=255;
            mLighting=false;
        }
        setAlpha(mAlpha);
        mPath.reset();
        // top left
        mPath.moveTo(mPointX - mSize * 0.5f, mPointY-mSize*0.16f);
        // top right
        mPath.lineTo(mPointX + mSize * 0.5f, mPointY-mSize*0.16f);
        // bottom left
        mPath.lineTo(mPointX - mSize * 0.32f, mPointY + mSize * 0.45f);
        // top tip
        mPath.lineTo(mPointX, mPointY - mSize * 0.5f);
        // bottom right
        mPath.lineTo(mPointX + mSize * 0.32f, mPointY + mSize * 0.45f);
        // top left
        mPath.lineTo(mPointX - mSize * 0.5f, mPointY-mSize*0.16f);
        mPath.close();
        canvas.drawPath(mPath, mPaint);
    }

    @Override
    public void setAlpha(int alpha) {
        mPaint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter colorFilter) {
        
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }
}
